package com.leostadyn.stackfarm.timestatistics.view.activity.admin.staff;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.base.BaseActivity;
import com.leostadyn.stackfarm.timestatistics.model.bean.JobBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.PostBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.UpdateInfoBean;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class StaffEditActivity extends BaseActivity implements View.OnClickListener {
    private TextView nav_title;
    private EditText ed_name;
    private TextView tv_job;
    private EditText ed_user_name;
    private Button btn_save;
    private static final int GROUP = 0x126;
    private TextView btn_add;
    private String mIndex = "-1";
    private List<PostBean> listdata = new ArrayList<>();
    private List<JobBean> jobBeanList = new ArrayList<>();
    private String username, job, name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staff_edit);
        username = getIntent().getStringExtra("username");
        job = getIntent().getStringExtra("job");
        name = getIntent().getStringExtra("name");
        initData();
        initView();
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            checkButton();
        }
    };

    //初始化数据
    public void initData() {
    }

    boolean checkUserTextLength() {
        int accountLength = ed_user_name.getText().toString().length();
        return accountLength < 30;
    }

    boolean checkNameLength() {
        int nameLength = ed_name.getText().toString().length();
        return nameLength <= 10;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
           if (GROUP == requestCode) {
                if (data != null) {
                    mIndex = data.getStringExtra("mIndex");
                    String info = data.getStringExtra("info");
                    tv_job.setText(info);
                    checkButton();
                }
            }
        }
    }

    private void checkButton() {
        if (isFill()) {
            btn_save.setEnabled(true);
        } else {
            btn_save.setEnabled(false);
        }
    }

    public boolean isFill() {
        String staff_edit_id = ed_name.getText().toString().trim();
        if (TextUtils.isEmpty(staff_edit_id)) {
            return false;
        }

        String staff_edit_user_name = ed_user_name.getText().toString().trim();
        return !TextUtils.isEmpty(staff_edit_user_name);
    }


    public void getIndex(String job) {
        HashMap<String, String> queryMap = new HashMap<>();
        AdminApi.getInstance().getJob().subscribe(new OriginalSubscriber<List<JobBean>>(compositeDisposable) {

            @Override
            public void onError(ResponseThrowable e) {

            }

            @Override
            public void onNext(List<JobBean> jobBeans, String msg) {
                jobBeanList = jobBeans;
                if (jobBeanList != null && jobBeanList.size() > 0) {
                    for (JobBean job : jobBeanList) {
                        queryMap.put(job.getName(), job.getId());
                    }
                    mIndex = queryMap.get(job);
                }

            }
        });

    }
    public void initView() {


        getIndex(job);
        nav_title = findViewById(R.id.nav_txt_title);
        nav_title.setText("编辑人员");

        btn_save = findViewById(R.id.staff_edit_save_btn);
        btn_save.setOnClickListener(this);
        findViewById(R.id.nav_img_back).setOnClickListener(v -> finish());
        tv_job = findViewById(R.id.staff_edit_tv_job);
        ed_name = findViewById(R.id.staff_ed_name);
        ed_user_name = findViewById(R.id.satff_ed_user_name);
        ed_name.setText(name);
        tv_job.setText(job);
        ed_user_name.setText(username);


        ed_name.addTextChangedListener(mTextWatcher);
        tv_job.addTextChangedListener(mTextWatcher);
        ed_user_name.addTextChangedListener(mTextWatcher);


    }

    public void onSelectJob(View view) {
        startActivityForResult(new Intent(this, JobListActivity.class).putExtra("mIndex", mIndex), GROUP);
    }


    @Override
    protected void onResume() {
        super.onResume();
//        checkButton();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.staff_edit_save_btn:
                if (!checkNameLength()) {
                    ToastUtil.show(this, "请输入有效姓名");
                    return;
                } else {
                    if (!checkUserTextLength()) {
                        ToastUtil.show(this, "用户名超过限制");
                    }
                }
                editStaff();
        }

    }

    public void editStaff() {
        String id = getIntent().getStringExtra("uid");
        String username = ed_user_name.getText().toString().trim();
        String name = ed_name.getText().toString().trim();


        HashMap<String, String> map = new HashMap<>();
        map.put("uid", id);
        map.put("userName", username);
        map.put("name", name);
        map.put("postId", mIndex);


        AdminApi.getInstance().updateInfo(map).subscribe(new OriginalSubscriber<UpdateInfoBean>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {

                ToastUtil.show(StaffEditActivity.this, e.getMsg());
            }

            @Override
            public void onNext(UpdateInfoBean updateInfoBean, String msg) {
                ToastUtil.show(StaffEditActivity.this, "编辑成功");
                Intent intent = new Intent();
                intent.putExtra("success", 1);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });


    }

}
